function generateTanConfiguration(path)
% this function reads the .vol file, and convert information to .tan file 
    file_list = dir([ path '/*.vol']);
    for i_file = 1:length(file_list)
       labels = parseVolFile([path '/' file_list(i_file).name]);
       A = eye(3);
       B = [labels.x_axis',labels.y_axis',labels.z_axis'];
       R=A/B;
       labels.R = R; % rotation from world coordinate system to volume coordinate system, R * B = A
       if strcmp(file_list(i_file).name,'vol_lid.vol');
	  labels.swt = 1/200; % scale from world/volume unit to tan unit, world is in terms of mm.
       elseif strcmp(file_list(i_file).name,'vol_roof.vol')
	  labels.swt = 1/400;
       else
          labels.swt = 1/150;% manually assign
       end
       new_dimension = abs(R*(labels.size'))';
       direction = labels.x_axis+labels.y_axis+labels.z_axis;
       % enlarge the root volum size by +- 5 percent
             
       d_dimension = 0.025*new_dimension;
       d_dimension = (inv(R)*d_dimension')';
       labels.orgin = labels.origin - d_dimension;  
       new_dimension = 1.05*new_dimension;
  
	   
	         
       labels.new_size = ceil(new_dimension*labels.swt);
       %banlace the offsze by rounding to each side by adjusting the origin%
       size_diff = labels.new_size/labels.swt-new_dimension;
       size_diff = (inv(R)*size_diff')';
       %disp(file_list(i_file).name);
       %disp(num2str(labels.origin));
    
       labels.origin = labels.origin-size_diff/2;
       
       %disp(num2str(direction));
       %disp(num2str(-size_diff/2));
       %disp(num2str(labels.origin));
       
	   
       writeTanFile(path, labels);
    end
end


function state = writeTanFile(path, labels)
    state = true;
    file_name = [path '/' labels.part_name '.tan'];
    pFile = fopen(file_name,'w');
    if ~pFile
        fprintf('can not create file : %s\n', file_name);
        state = false;
    end
    string = '%1.root volume size along three axis ';
    fprintf(pFile,'%s\n',string);
    string = num2str(labels.new_size, '%0d,');
    fprintf(pFile,'%s\n',string);
    
    string = '%2.lower-left-inner corner of the volume on world coordinate system ';
    fprintf(pFile,'%s\n',string);
    string = num2str(labels.origin, '%0.5g,');
    fprintf(pFile,'%s\n',[string]);
    
    string = '%3.aspect ratio limit on volumes, lower bound ';
    fprintf(pFile,'%s\n',string);
    string = '1,1,0.2 ';
    fprintf(pFile,'%s\n',string);
    
    string = '%4.aspect ratio limit on volumes, upper bound ';
    fprintf(pFile,'%s\n',string);
    string = '3,3,0.4 ';
    fprintf(pFile,'%s\n',string);
    
    string = '% 5.affine transform matrix, including rotation,  % a 3x3 matrix, columnwise, shifting is derived from the corner point. ';
    fprintf(pFile,'%s\n',string);
    string = num2str(labels.R(:)', '%0.5g,');
    fprintf(pFile,'%s\n',[string ]);

    string = '% 6. scale from world to tan coordiante system swt ';
    fprintf(pFile,'%s\n',string);
    string = labels.swt;
    fprintf(pFile,'%s\n',string);
 
    string = '% 7. scale from tan coordinte system to plane coordinate system. ';
    fprintf(pFile,'%s\n',string);
    string = '10 ';
    fprintf(pFile,'%s\n',string);
    
    fclose(pFile);
    
end


function labels =parseVolFile(filename)
% read information in .vol file
pFile = fopen(filename);
if ~pFile
    fprintf('can not open file : %s\n', filename);
end
i_line = 0;
txt_line = fgetl(pFile);
while 1
   txt_line = fgetl(pFile);
   if  isempty(txt_line) | (txt_line ==-1)
    break;
   end
    if (txt_line(1)=='%') % the sign of comment
        continue;
    end
    data = sscanf(txt_line,'%f,')';
    i_line = i_line+1;
    switch i_line
        case 1 % root tan name
            labels.part_name = txt_line;
        case 2 % origin position in mm
            labels.origin = data*25.4;
        case 3 % x axis
            labels.x_axis = data;
        case 4 % y axis
            labels.y_axis = data;
        case 5 % z axis
            labels.z_axis = data;
        case 6 % size in mm
            labels.size = data;
        otherwise
            disp('unknown parameter for root vol config file ignored')
    end
end

fclose(pFile);
end
